package com.luckku.boot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.luckku.boot.common.api.ApiResult;
import com.luckku.boot.model.entity.Cart;
import com.luckku.boot.model.vo.CartInfoVo;
import com.luckku.boot.service.ICartService;
import com.luckku.boot.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author luckku
 * @date 2021/12/15
 */

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    ICartService cartService;

    @Autowired
    SnowFlakeUtil snowFlakeUtil;

    /**
     * /cart/userCart
     * 获取用户购物车
     */
    @ResponseBody
    @GetMapping("/userCart")
    public ApiResult<CartInfoVo> getUserCart(@RequestParam("userId") String userId, @RequestParam("storeId") String storeId) {
        CartInfoVo cartInfoVO = cartService.getUserCart(userId, storeId);
        return ApiResult.success(cartInfoVO);
    }

    /**
     * /cart/cleanCart
     * 清空购物车
     */
    @ResponseBody
    @DeleteMapping("/cleanCart")
    public ApiResult<String> cleanCart(@RequestParam("userId") String userId, @RequestParam("storeId") String storeId) {
        cartService.deleteCart(userId, storeId);
        return ApiResult.success("","已清空购物车");
    }

    /**
     * /cart/updateCount
     * 更新用户购物车数量
     */
    @ResponseBody
    @PostMapping("/updateCount")
    public ApiResult<Object> updateCount(@RequestParam("cartId") String cartId, @RequestParam("count") Long count) {
        cartService.update(
                new UpdateWrapper<Cart>()
                        .eq("cart_id",cartId)
                        .set("count",count)
        );
        return ApiResult.success(null,"修改数量成功");
    }
    /**
     * /cart/addToCart
     * 向购物车中添加产品
     */
    @ResponseBody
    @PostMapping("/addToCart")
    public ApiResult<Object> addToCart(@RequestParam("storeId") String storeId, @RequestParam("userId") String userId, @RequestParam("goodsId") String goodsId,  @RequestParam("count") Long count) {
        Cart cart = cartService.getOne(
                new QueryWrapper<Cart>().eq("goods_id", goodsId).eq("store_id", storeId)
        );
        if (cart == null) {
            cartService.save(
                    Cart.builder()
                    .cartId(String.valueOf(snowFlakeUtil.snowflakeId()))
                    .goodsId(goodsId).count(count).userId(userId).storeId(storeId)
                    .build()
            );
        } else {
            cartService.update(
                    new UpdateWrapper<Cart>()
                            .eq("cart_id",cart.getCartId())
                            .set("count",cart.getCount()+count)
            );
        }

        return ApiResult.success(null,"添加成功");
    }

    /**
         * /cart/deleteGoods
     * 删除购物车商品
     */
    @ResponseBody
    @DeleteMapping("/deleteGoods")
    public ApiResult<Object> deleteGoods(@RequestParam("cartId") String cartId) {
        cartService.remove(
                new LambdaQueryWrapper<Cart>()
                        .like(Cart::getCartId, cartId)
        );
        return ApiResult.success(null,"删除商品成功");
    }


}
